package com.jeeplus.test.jlnu.deanOffice.util;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.StudentInfoEntity;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.StudentsOffice;
import com.jeeplus.test.jlnu.admissionInfo.service.StudentsOfficeService;
import com.jeeplus.test.jlnu.deanOffice.domain.entity.JlnuDeansOffice;
import com.jeeplus.test.jlnu.deanOffice.service.DeansOfficeService;

import java.util.ArrayList;
import java.util.List;

public class DeanOfficeDataListener extends AnalysisEventListener<JlnuDeansOffice> {
    private  DeansOfficeService deansOfficeService;

    private  DeanOfficeDataListener.ProgressCallback progressCallback;
    private static final int BATCH_COUNT = 500;
    private  String officeId;

    private List<JlnuDeansOffice> dataList = new ArrayList<>();
    private int totalRecords;
    private int currentRecords;



//    private void saveDataToDatabase() {
//        if (!dataList.isEmpty()) {
//            deansOfficeService.addJlnuDeansOffice(dataList);
//        }
//
//    }

    @Override
    public void invoke(JlnuDeansOffice jlnuDeansOffice, AnalysisContext analysisContext) {
        // 获取当前行号
        int currentRow = analysisContext.readRowHolder().getRowIndex();

        // 判断当前是否是第二行，跳过第二行
        if (currentRow == 1) {
            return; // 跳过第二行
        }
//        jlnuDeansOffice.setOfficeId(officeId);
        dataList.add(jlnuDeansOffice);
        currentRecords++;

        if (dataList.size() >= BATCH_COUNT) {
//            saveDataToDatabase();
            dataList.clear();
        }

        updateProgress();
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (!dataList.isEmpty()) {
//            saveDataToDatabase();
            dataList.clear();
        }
        updateProgress();
    }

//    private void saveDataToDatabase() {
//        if (!dataList.isEmpty()) {
//            deansOfficeService.addJlnuDeansOffice(dataList);
//        }
//    }

    private void updateProgress() {
        int progress = (int) (((double) currentRecords / totalRecords) * 100);
//        progressCallback.onProgress(progress);
    }

    public interface ProgressCallback {
        void onProgress(int progress);
    }
}
